package com.quizzer.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.quizzer.common.utils.DateUtils;
import com.quizzer.system.convert.CategoryConverter;
import com.quizzer.system.dto.question.category.CategoryAddDto;
import com.quizzer.system.dto.question.category.CategoryOutputDto;
import com.quizzer.system.dto.question.category.CategoryQueryDto;
import com.quizzer.system.dto.question.category.CategoryUpdateDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.quizzer.system.mapper.CategoryMapper;
import com.quizzer.system.domain.Category;
import com.quizzer.system.service.ICategoryService;

/**
 * 小类别Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-07
 */
@Service
public class CategoryServiceImpl implements ICategoryService
{
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查询小类别
     *
     * @param id 小类别主键
     * @return 小类别
     */
    @Override
    public CategoryOutputDto selectCategoryById(Long id)
    {
        Category category = categoryMapper.selectCategoryById(id);
        return selectOne(category);
    }

    private CategoryOutputDto selectOne(Category category) {
        return CategoryConverter.toOutputDto(category);
    }

    /**
     * 查询小类别列表
     *
     * @param queryDto 小类别
     * @return 小类别
     */
    @Override
    public List<CategoryOutputDto> selectCategoryList(CategoryQueryDto queryDto)
    {
        Category queryCategory = CategoryConverter.toEntity(queryDto);
        List<Category> categoryList = categoryMapper.selectCategoryList(queryCategory);
        List<CategoryOutputDto> outputDtoList = new ArrayList<>();
        for (Category category : categoryList) {
            outputDtoList.add(selectOne(category)   );
        }
        return outputDtoList;
    }

    /**
     * 新增小类别
     *
     * @param addDto 小类别
     * @return 结果
     */
    @Override
    public int insertCategory(CategoryAddDto addDto)
    {
        Category category = CategoryConverter.toEntity(addDto);
        return categoryMapper.insertCategory(category);
    }

    /**
     * 修改小类别
     *
     * @param category 小类别
     * @return 结果
     */
    @Override
    public int updateCategory(CategoryUpdateDto updateDto)
    {
        Category category = CategoryConverter.toEntity(updateDto);
        return categoryMapper.updateCategory(category);
    }

    /**
     * 批量删除小类别
     *
     * @param ids 需要删除的小类别主键
     * @return 结果
     */
    @Override
    public int deleteCategoryByIds(Long[] ids)
    {
        return categoryMapper.deleteCategoryByIds(ids);
    }

    /**
     * 删除小类别信息
     *
     * @param id 小类别主键
     * @return 结果
     */
    @Override
    public int deleteCategoryById(Long id)
    {
        return categoryMapper.deleteCategoryById(id);
    }
}
